//
//  HZPeiziAccountMoneyView.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/11/30.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziAccountMoneyView.h"
#import "HZUpdownLable.h"
#import "HZPeiziAccountMoneyTableViewCell.h"

@interface HZPeiziAccountMoneyView()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UIView *pHeaderView;
@property (nonatomic, strong)HZUpdownLable *pWarningLine;
@property (nonatomic, strong)HZUpdownLable *pForceLine;
@property (nonatomic, strong)HZUpdownLable *pCurMoney;
@property (nonatomic, strong)UITableView *pTableView;
@property (nonatomic, strong)NSArray *pDataList;

@property (nonatomic, strong)NSArray *pLeftNameArray;
@property (nonatomic, strong)UIImageView *pStateImageView;
@end

@implementation HZPeiziAccountMoneyView

#pragma mark -
#pragma mark 生命周期
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
        [self initCommon];
        [self initConstraints];
    }
    return self;
}
- (void)initCommon
{
    _pHeaderView = [UIView new];
    _pWarningLine = [HZUpdownLable new];
    _pForceLine = [HZUpdownLable new];
    _pCurMoney = [HZUpdownLable new];
    _pTableView = [UITableView new];
    _pTableView.dataSource = self;
    _pTableView.delegate = self;
    _pTableView.tableFooterView = [UIView new];
    _pStateImageView = [UIImageView new];
    
    _pTableView.bounces = NO;
    [_pWarningLine setUptext:@"--" downtext:@"警戒线"];
    [_pForceLine setUptext:@"--" downtext:@"强平线"];
    [_pCurMoney setUptext:@"--" downtext:@"当前资产"];
    [_pWarningLine setUpTextColor:[UIColor blackColor]];
    [_pForceLine setUpTextColor:[UIColor blackColor]];
    [_pCurMoney setUpTextColor:[UIColor blackColor]];
    _pHeaderView.backgroundColor = [UIColor whiteColor];
    
    _pLeftNameArray = @[@"初始资金",@"资产总值",@"持仓市值",@"资金余额",@"可用资金",@"冻结资金"];
}
- (void)initConstraints
{
    [self addSubview:_pHeaderView];
    [_pHeaderView addSubview:_pWarningLine];
    [_pHeaderView addSubview:_pForceLine];
    [_pHeaderView addSubview:_pCurMoney];
    [_pHeaderView addSubview:_pStateImageView];
    [self addSubview:_pTableView];
    
    [_pHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self);
        make.height.mas_equalTo(100);
    }];
    [_pWarningLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pHeaderView).offset(25);
        make.left.mas_equalTo(_pHeaderView).offset(10);
        make.bottom.mas_equalTo(_pHeaderView).offset(-10);
        make.right.mas_equalTo(_pForceLine.mas_left);
    }];
    [_pForceLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(_pWarningLine);
        make.right.mas_equalTo(_pCurMoney.mas_left);
    }];
    [_pCurMoney mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(_pForceLine);
        make.right.mas_equalTo(_pHeaderView).offset(-10);
        make.width.mas_equalTo(@[_pWarningLine, _pForceLine]);
    }];
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pHeaderView.mas_bottom).offset(20);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(30*6);
    }];
    [_pStateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(_pHeaderView);
        make.height.mas_equalTo(_pHeaderView).multipliedBy(0.5);
        make.width.mas_equalTo(50);
    }];
}

- (void)updateAccountData:(HZPeiziAccountMoneyModel *)model
{
    if (!model.p_init_balance) {
        return;
    }
    if ([model.stat isEqual:@"正常"]) {
        _pStateImageView.image = [UIImage imageNamed:@"safe"];
    }else if ([model.stat isEqual:@"限仓"]) {
        _pStateImageView.image = [UIImage imageNamed:@"warning"];
    }else {// 爆仓和穿仓
        _pStateImageView.image = [UIImage imageNamed:@"shit"];
    }
    
    [_pWarningLine setUptext:model.fml];
    [_pForceLine setUptext:model.fme];
    [_pCurMoney setUptext:[NSString stringWithFormat:@"%0.2f", [model.esum floatValue]]];
    
    NSArray *dataArray = @[model.p_init_balance,
                           [NSString stringWithFormat:@"%0.2f", [model.total_assert floatValue]],
                           model.mak_val,
                           [NSString stringWithFormat:@"%0.2f", [model.balance floatValue]],
                           [NSString stringWithFormat:@"%0.2f", [model.valid_balance floatValue]],
                           model.frozen_balance];
    
    NSMutableArray *pTempArray = [NSMutableArray array];
    [_pLeftNameArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        HZAccountMoneyModel *model = [HZAccountMoneyModel new];
        model.mode_name = obj;
        model.mode_text = dataArray[idx];
        [pTempArray addObject:model];
    }];
    _pDataList = pTempArray;
    [_pTableView reloadData];
}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _pDataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UITableViewCell *cell = nil;
    HZAccountMoneyModel *model = _pDataList[indexPath.row];
    cell = [HZPeiziAccountMoneyTableViewCell cellTableView:tableView indexPath:indexPath cellModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30;
}

@end
